package cn.iocoder.yudao.module.scrm.controller.admin.teacher;

import cn.iocoder.yudao.framework.common.enums.CommonGenderEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.scrm.controller.admin.teacher.vo.TeacherPageReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.teacher.vo.TeacherRespVO;
import cn.iocoder.yudao.module.scrm.controller.admin.teacher.vo.TeacherSaveReqVO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.teacher.TeacherDO;
import cn.iocoder.yudao.module.scrm.service.teacher.TeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 老师信息")
@RestController
@RequestMapping("/scrm/teacher")
@Validated
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    @PostMapping("/create")
    @Operation(summary = "创建老师信息")
    @PreAuthorize("@ss.hasPermission('scrm:teacher:create')")
    public CommonResult<Long> createTeacher(@Valid @RequestBody TeacherSaveReqVO createReqVO) {
        return success(teacherService.createTeacher(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新老师信息")
    @PreAuthorize("@ss.hasPermission('scrm:teacher:update')")
    public CommonResult<Boolean> updateTeacher(@Valid @RequestBody TeacherSaveReqVO updateReqVO) {
        teacherService.updateTeacher(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除老师信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('scrm:teacher:delete')")
    public CommonResult<Boolean> deleteTeacher(@RequestParam("id") Long id) {
        teacherService.deleteTeacher(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得老师信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('scrm:teacher:query')")
    public CommonResult<TeacherRespVO> getTeacher(@RequestParam("id") Long id) {
        TeacherDO teacher = teacherService.getTeacher(id);
        return success(BeanUtils.toBean(teacher, TeacherRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得老师信息分页")
    @PreAuthorize("@ss.hasPermission('scrm:teacher:query')")
    public CommonResult<PageResult<TeacherRespVO>> getTeacherPage(@Valid TeacherPageReqVO pageReqVO) {
        PageResult<TeacherDO> pageResult = teacherService.getTeacherPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TeacherRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出老师信息 Excel")
    @PreAuthorize("@ss.hasPermission('scrm:teacher:export')")
    @OperateLog(type = EXPORT)
    public void exportTeacherExcel(@Valid TeacherPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TeacherDO> list = teacherService.getTeacherPage(pageReqVO).getList();
        //设置创建人信息-男女性别
        List<TeacherRespVO> bean = BeanUtils.toBean(list, TeacherRespVO.class);
        bean.forEach(item->item.setSex(CommonGenderEnum.getGenderByCode(item.getSex())));
        // 导出 Excel
        ExcelUtils.write(response, "老师信息.xls", "数据", TeacherRespVO.class,
                       bean );
    }

}